//
//  SearchRecommendView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/2/26.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

protocol SearchRecommendViewDelegate:NSObjectProtocol {
    func SearchRecommendViewClick(name:String)
}

class SearchRecommendView: UIView {
    weak var delegate:SearchRecommendViewDelegate?
    let SearchCollectionViewCellId = "SearchCollectionViewCell"
    //    fileprivate var viewCT:UIViewController!
    
    lazy var listModel: [HotSearchWordModel] = [{
        return HotSearchWordModel()
        }()]
    
    fileprivate lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        //        layout.itemSize = CGSize.init(width: SCREEN_WIDTH / 5, height: 70)
        layout.sectionInset = UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = 15
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.register(SearchCollectionViewCell.self, forCellWithReuseIdentifier: SearchCollectionViewCellId)
        collectionView.delegate = self
        collectionView.dataSource = self
        return collectionView
    }()
    
    //    var itemsModel:[CentreItemImagesModel]
    
    init(frame: CGRect, viewController:UIViewController) {
        super.init(frame: frame)
        self.collectionView.frame = frame
        self.collectionView.backgroundColor = WhiteBackColor
        self.addSubview(self.collectionView)
        
        setData()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//  MARK: - ###### 私有方法 ######
extension SearchRecommendView {
    func setCollectionView() {
        
    }
    
}


//  MARK: - ###### 公有方法 ######
extension SearchRecommendView {
    func setData() {
        HomeRequestModel.loadHotSeatchWordData { (success, message, model) in
            if success && model != nil {
                self.listModel = model!
                self.collectionView.reloadData()
            }
        }
    }
    
}

//  MARK: - ###### 协议方法 ######
extension SearchRecommendView:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.listModel.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SearchCollectionViewCellId, for: indexPath) as! SearchCollectionViewCell
        cell.titleStr = self.listModel[indexPath.row].name
        cell.titleBtn.text = self.listModel[indexPath.row].name
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        let name:String = self.model?[indexPath.row].name ?? ""
        let name = self.listModel[indexPath.row].name
        delegate?.SearchRecommendViewClick(name: name ?? "")
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: setHeight(str: self.listModel[indexPath.row].name ?? ""   ), height: 28)
    }
    
    func setHeight(str:String) -> CGFloat {
        let num = str.count
        let count = 30 + 15 * num
        return CGFloat(count)
    }
    
}

//  MARK: - ###### 私有方法 ######
extension HomeItemsView {
    
}
